import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class E09_SimplePriorities {

	public static void main(String[] args) {
		ExecutorService exec = null;
		for (int i = 0; i < 5; i++) {
			exec = Executors.newCachedThreadPool(new PrioritiesThreadFactory(Thread.MAX_PRIORITY));
			exec.execute(new SimplePriorities());
			exec = Executors.newCachedThreadPool(new PrioritiesThreadFactory(Thread.MIN_PRIORITY));
			exec.execute(new SimplePriorities());
		}
		exec.shutdown();
	}

}

class SimplePriorities implements Runnable {
	private int countDown = 5;
	private volatile double d;

	@Override
	public String toString() {
		return Thread.currentThread() + ":" + countDown;
	}

	@Override
	public void run() {
		while (true) {
			for (int i = 0; i < 100000; i++) {
				d = (Math.PI + Math.E) / (double) i;
				if (i % 10000 == 0) {
					Thread.yield();
				}
			}
			System.out.println(this);
			if (--countDown == 0) {
				return;
			}
		}
	}
}

class PrioritiesThreadFactory implements ThreadFactory {
	private final int priority;

	PrioritiesThreadFactory(int priority) {
		this.priority = priority;
	}

	@Override
	public Thread newThread(Runnable r) {
		Thread t = new Thread(r);
		t.setPriority(priority);
		return t;
	}

}